package offer_test_20;

import struct.BinaryTree;
import struct.BinaryTreeNode;

//请完成一个函数，输入一个二叉树，该函数输出它的镜像。
public class nintenn_MirrorofBinaryTree {
	public static void main(String[] args) {
		BinaryTreeNode root1 = new BinaryTreeNode();
		BinaryTreeNode node1 = new BinaryTreeNode();
		BinaryTreeNode node2 = new BinaryTreeNode();
		BinaryTreeNode node3 = new BinaryTreeNode();
		BinaryTreeNode node4 = new BinaryTreeNode();
		BinaryTreeNode node5 = new BinaryTreeNode();
		BinaryTreeNode node6 = new BinaryTreeNode();
		root1.leftNode = node1;
		root1.rightNode = node2;
		node1.leftNode = node3;
		node1.rightNode = node4;
		node4.leftNode = node5;
		node4.rightNode = node6;
		root1.value = 8;
		node1.value = 8;
		node2.value = 7;
		node3.value = 9;
		node4.value = 2;
		node5.value = 4;
		node6.value = 7;
		BinaryTreeNode rootBinaryTreeNode = mirror(root1);
		// System.out.println(rootBinaryTreeNode.leftNode.value);
		BinaryTree.printInorder(rootBinaryTreeNode);
	}

	// 递归
	public static BinaryTreeNode mirror(BinaryTreeNode node) {
		if (node == null) {
			return null;
		}
		if (node.rightNode != null || node.leftNode != null) {
			BinaryTreeNode temp = node.rightNode;
			node.rightNode = node.leftNode;
			node.leftNode = temp;
		}
		if (node.rightNode != null) {
			node.rightNode = mirror(node.rightNode);
		}
		if (node.leftNode != null) {
			node.leftNode = mirror(node.leftNode);
		}
		return node;
	}
}
